package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-07-21 19:43:22
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{

    @Autowired
    private TbTypeMapper tbTypeMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Override
    public R getTypeList(PageInfoVo pageInfoVo) {

        QueryWrapper<TbType> wrapper = new QueryWrapper<>();
        if(pageInfoVo.getKeyWord()!=null && pageInfoVo.getKeyWord()!=""){
            wrapper.lambda().like(TbType::getTypeName,pageInfoVo.getKeyWord());
        }
        if(pageInfoVo.getTypeId()!=null){
            wrapper.lambda().eq(TbType::getTypeStatus,pageInfoVo.getTypeId());
        }
        if(pageInfoVo.getKeyWord()!=null && pageInfoVo.getKeyWord()!="" || pageInfoVo.getTypeId()!=null){
            List<TbType> tbTypes = tbTypeMapper.selectList(wrapper);
            return R.OK(tbTypes);
        }

        List<TbType> tbTypes = tbTypeMapper.selectList(null);

        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (tree, menu) -> {
            menu.setId(tree.getTypeId() + "");
            menu.setName(tree.getTypeName());
            menu.setParentId(tree.getTypePid() + "");
            menu.putExtra("typeId", tree.getTypeId());
            menu.putExtra("typeName",tree.getTypeName());
            menu.putExtra("typeImage", tree.getTypeImage());
            menu.putExtra("typeTop", tree.getTypeTop());
            menu.putExtra("typeStatus",tree.getTypeStatus());
        });

        return R.OK(treeList);
    }

    @Override
    public R upload(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath path = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+path.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }
    }

    @Override
    public R addType(TbType tbType) {
        TbType tbType1 = tbTypeMapper.selectOne(new QueryWrapper<TbType>().lambda().eq(TbType::getTypeName, tbType.getTypeName()));
        if(tbType1!=null){
            return R.ERROR(500,"该类型已存在");
        }
        tbTypeMapper.insert(tbType);
        return R.OK();
    }

    @Override
    public R getSelectTypeList() {
        List<TbType> tbTypes = tbTypeMapper.selectList(null);

        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (tree, menu) -> {
            menu.setId(tree.getTypeId()+"");
            menu.setParentId(tree.getTypePid()+"");
            menu.setName(tree.getTypeName());
            menu.putExtra("value",tree.getTypeId());
            menu.putExtra("label",tree.getTypeName());
        });
        return R.OK(treeList);
    }
}




